import { Dic } from '../../types';
import { DicRepository } from '../../repositories/system/DicRepository';
import { HttpError } from '../../exceptions/HttpError';
export class DicService {
    private dicRepository: DicRepository;

    constructor() {
        this.dicRepository = new DicRepository();
    }




    // 获取所有用户
    async getAllDics(): Promise<Dic[]> {
        try {
            return await this.dicRepository.findAll();
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`获取字典项列表失败: ${error.message}`);
        }
    }

    // 根据ID获取用户
    async getDicById(id: string): Promise<Dic | null> {
        try {
            return await this.dicRepository.findById(id);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`获取字典项信息失败: ${error.message}`);
        }
    }

    // 创建用户
    async createDic(dicData: Partial<Dic>): Promise<Dic> {
        try {
            return await this.dicRepository.create(dicData);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`创建字典项失败3: ${error.message}`);
        }
    }

    // 更新用户
    async updateDic(id: string, dicData: Partial<Dic>): Promise<Dic | null> {
        try {
            return await this.dicRepository.update(id, dicData);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`更新字典项失败: ${error.message}`);
        }
    }

    // 删除用户
    async deleteDic(id: string): Promise<boolean> {
        try {
            return await this.dicRepository.delete(id);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`删除字典项失败: ${error.message}`);
        }
    }

    // 根据邮箱查找用户（用于认证）
    async getDicByType(type: string): Promise<Dic[] | null> {
        try {
            return await this.dicRepository.findByType(type);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`查找字典项失败: ${error.message}`);
        }
    }

    async getDicByTypeAndValue(type: string, value: string): Promise<Dic[] | null> {
        try {
            return await this.dicRepository.findByTypeAndValue(type, value);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`查找字典项失败: ${error.message}`);
        }
    }




    // 获取用户数量
    async getDicCount(): Promise<number> {
        try {
            return await this.dicRepository.count();
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`统计字典项数量失败: ${error.message}`);
        }
    }

    // 分页查询用户
    async getDicsWithPagination(page: number = 1, limit: number = 10): Promise<{ list: Dic[], total: number, pages: number }> {
        try {
            return await this.dicRepository.findWithPagination(page, limit);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`分页查询字典项失败: ${error.message}`);
        }
    }
}